package com.lancoo.cvas.interceptor;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson2.JSON;
import com.lancoo.cvas.entity.TaskInfo;
import com.lancoo.cvas.pojo.common.Deleted;
import com.lancoo.cvas.pojo.vo.CurrentTermInfo;
import com.lancoo.cvas.service.TaskInfoService;
import com.lancoo.cvas.service.YunService;
import com.lancoo.cvas.util.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @Author: liufeng
 * @CreateTime: 2024-04-10  10:47
 * @Description: 同步课表到基础平台时  判断学期
 */
@Component
@Slf4j
public class SyncScheduleInterceptor implements HandlerInterceptor {
    @Resource
    private TaskInfoService taskInfoService;
    @Resource
    private YunService yunService;

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        if (!(handler instanceof HandlerMethod)){
            return true;
        }
        String taskId = request.getHeader("taskId");
        if (StrUtil.isEmpty(taskId)) {
            log.error("==================同步课表：请求地址： "
                    + request.getRequestURI()
                    + "taskId不存在");
            response.setContentType("application/json;charset=UTF-8");
            //设置编码格式
            response.setCharacterEncoding("UTF-8");
            response.setStatus(200);
            Result<Object> res = Result.ofFail("计划Id不存在");
            response.getWriter().write(JSON.toJSONString(res));
            return false;
        }

        TaskInfo taskInfo = taskInfoService.lambdaQuery()
                .eq(TaskInfo::getDeleted, Deleted.NOT.getCode())
                .eq(TaskInfo::getTaskId, Integer.valueOf(taskId))
                .one();
        if (taskInfo == null){
            log.error("==================同步课表：请求地址： "
                    + request.getRequestURI()
                    + "taskId： " + taskId
                    + " 不存在");
            response.setContentType("application/json;charset=UTF-8");
            //设置编码格式
            response.setCharacterEncoding("UTF-8");
            response.setStatus(200);
            Result<Object> res = Result.ofFail("计划Id不存在");
            response.getWriter().write(JSON.toJSONString(res));
            return false;
        }

        String schoolId = taskInfo.getSchoolId();
        CurrentTermInfo currentTermInfo = yunService.GetCurrentTermInfo(schoolId);
        if (!taskInfo.getTerm().equals(currentTermInfo.getTerm())) {
            log.error("==================同步课表：请求地址： "
                    + request.getRequestURI()
                    + "taskId： " + taskId
                    + " 不是当前学期");
            response.setContentType("application/json;charset=UTF-8");
            //设置编码格式
            response.setCharacterEncoding("UTF-8");
            response.setStatus(200);
            Result<Object> res = Result.ofFail("计划：" + taskInfo.getTaskName()
                    + " 不是当前学期，暂时不能同步");
            response.getWriter().write(JSON.toJSONString(res));
            return false;
        }

        log.info("==================同步课表：请求地址： "
                + request.getRequestURI()
                + "taskId： " + taskId
                + " 开始同步课表");
        return true;
    }
}
